package com.health.trans.pojo;

import cn.hutool.core.util.StrUtil;
import com.health.trans.annotation.FieldValid;
import com.health.trans.kit.ReaderConfig;
import com.health.trans.kit.WriterConfig;
import com.health.trans.util.EntityOperations;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import java.util.List;

/**
 * @author nqlz
 */
@Getter
@Setter
@Slf4j
@ApiModel(description = "表信息")
@FieldValid
@Accessors(chain = true)
public class TableInfo<T> {

    @ApiModelProperty("读表名")
    private String readTableName;
    @ApiModelProperty("写表名")
    private String writerTableName;

    @ApiModelProperty(value = "读表对应的类", hidden = true)
    private transient Class<T> readTableClass;

    @ApiModelProperty(value = "写表对应的类", hidden = true)
    private transient Class<T> writerTableClass;

    @ApiModelProperty(value = "查询条件字段信息")
    @Valid
    private List<FieldInfo> fieldInfos;

    @ApiModelProperty(hidden = true)
    private transient WriterConfig<T> writerConfig;
    @ApiModelProperty(hidden = true)
    private transient ReaderConfig<T> readerConfig;


    public void check() {
        Class<T> pojoClass = EntityOperations.getPojoClass(readTableName);
        if (StrUtil.isBlank(writerTableName)) {
            writerTableName = readTableName;
            writerTableClass = pojoClass;
            readTableClass = pojoClass;
        } else {
            writerTableClass = EntityOperations.getPojoClass(writerTableName);
        }
    }

    public WriterConfig<T> getWriterConfig() {
        if (StrUtil.isBlank(writerConfig.getTableName())){
            writerConfig.setTableClass(writerTableClass);
            writerConfig.setTableName(writerTableName);
        }
        return writerConfig;
    }
}
